package com.ruimei.controller;

import com.alibaba.fastjson.JSONArray;
import com.ruimei.entitys.ClassNews;
import com.ruimei.entitys.Teacher;
import com.ruimei.service.ClassNewsService;
import com.ruimei.util.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags={"班级动态"})
@Controller
public class ClassNewsController {
    @Resource
    ClassNewsService service;
    @Resource
    private RedisTemplate<String,String> redisTemplate;


    @PostMapping({"/classnews/add"})
    @ResponseBody
    @ApiOperation("添加动态")
    public Map<String, Object> addClassNews(ClassNews classNews, HttpServletRequest request)
    {
        Teacher teacher = new Teacher();
        teacher.setId(TokenUtils.getTeacherId(request));
        if(redisTemplate.hasKey(teacher.getId())){
            teacher = JSONArray.parseObject(redisTemplate.opsForValue().get(teacher.getId()),Teacher.class);
            classNews.setTeacherId(teacher.getId());
            classNews.setTeacherName(teacher.getFullName());

            Map<String, Object> map = service.addClassNews(classNews);
            return map;
        }
        return null;
    }

    @PostMapping({"/classnews"})
    @ResponseBody
    @ApiOperation("班级动态")
    public Map<String, Object> getClassNews(ClassNews classNews)
    {
        Map<String, Object> map = service.getClassNews(classNews);
        return map;
    }

    @PostMapping({"/classnews/teacher"})
    @ResponseBody
    @ApiOperation("班级动态")
    public Map<String, Object> getClassNews(String classId, String beginTime,String endTime)
    {
        Map<String, Object> map = service.getClassNews(classId,beginTime,endTime);
        return map;
    }

}
